class ChannelsController < ApplicationController
  #权限验证
  before_filter :auth
  layout "admin"
  def index
    redirect_to :action=>'list'
  end

  def new   
    @channel = Channel.new
    @titles = "新增频道"
  end


  def create   
    @channel = Channel.new(params[:channel])    
    channel = Channel.find_all_by_orders @channel[:orders]
    if  @channel && @channel.save && !channel.nil? && @channel.errors.empty?
      flash[:notice] = '搞定一个频道！'
      redirect_to :action=>'list'
    else
      render :action => "new"
    end
  end


  #查询
  def list
    #提交表单为post
    if request.post?
      @channle_search = params[:channel]
      name = @channle_search[:name]
      @channels = Channel.paginate :per_page => $perPage, :page =>  params[:page],
        :conditions => ['name like ? ', "%#{name}%"], :order => 'orders asc'
    else
      @channels = Channel.paginate  :page => params[:page],
        :per_page => $perPage,       
        :order => "orders asc"
    end    
    #查询项参数
    @channel = Channel.new
    @channel.name = name     
    #功能名
    @titles = "频道列表"    
  end

  def edit
    @channel = Channel.find(params[:id])
    @titles = "编辑频道---"+@channel.name
  end

  def update
    @channel = params[:channel]
    if Channel.update(@channel[:id], @channel)
      flash[:notice] = '更新成功'
      redirect_to :action=>'index'
    else
      render :action => "edit"
    end
  end

  def show
    @channel = Channel.find(params[:id])
    @titles = "频道详细信息---"+@channel.name
  end

  def delete
   channel = Channel.find(params[:id])
   secs = Section.find_all_by_channel_id channel.id
   if secs.size==0
     channel.destroy
   end   
   redirect_to :action=>'index'
  end
  
end